﻿package 
{
	//WEEK 05
	import flash.display.MovieClip;
	import flash.display.FrameLabel;
	import flash.events.*;
	import flash.text.TextField;
	import flash.ui.Keyboard;
	import flash.geom.Point;
	//bullet class
	import bullet;
	

	public class GameManager extends MovieClip{
		static public const MENU:String = "menu";
		static public const GAME:String = "game";
		static public const END:String = "end";		
	
		// constant for the speed of the bullet
		public const BULLETPOWER:int = 9;
		
		//wk5 guard list 
		public var guards:Array = new Array();
		
		//holds a reference to the bullet when it is there
		public var bulletRef:bullet = null;	
		
		
		public function GameManager(){
			//setup frames of main movie to send an event when they are reached
			enumerateFrameLabels();
			addEventListener(MENU, setupMenu,false,0,true);
			addEventListener(GAME, setupGame,false,0,true);
			addEventListener(END, setupEnd,false,0,true);
		}
		
		private function dispatchFrameEvent():void{
			stop();
			dispatchEvent(new Event(currentLabel));
		}
		private function enumerateFrameLabels():void{
			for each (var label:FrameLabel in currentLabels)
				addFrameScript(label.frame-1, dispatchFrameEvent);
		}
		private function setupMenu(e:Event){
			test_button.button_text.text = "Start";
			test_button.addEventListener(MouseEvent.CLICK, aClick,false,0,true);
		}
		
		private function setupGame(e:Event){
			end_button.button_text.text = "Win";
			end_button.addEventListener(MouseEvent.CLICK, bClick,false,0,true);
			// add the main game loop listener
			addEventListener(Event.ENTER_FRAME,update,false,0,true);

			//wk5 add the guard movie clips to the guards list
			guards.push(guard01);
			guards.push(guard02);
		}
		private function setupEnd(e:Event){
			
			start_button.button_text.text = "Menu";
			start_button.addEventListener(MouseEvent.CLICK, cClick,false,0,true);
			
		}
		
		private function cleanUpGame()
		{

			//remove Enter frame listener
			removeEventListener(Event.ENTER_FRAME,update);
		}
		
		//made public
		public function exitGame()
		{
			cleanUpGame();
			gotoAndPlay(END);
		}
		

		//START Button Management Code		
		private function aClick(e:Event){
			gotoAndPlay(GAME);
		}
		private function bClick(e:Event){
			gotoAndPlay(END);
		}
		private function cClick(e:Event){
			gotoAndPlay(MENU);
		}
		//END Button Management
		

	
		//MAIN GAME LOOP
		private function update(e:Event)
		{
			character.nextPosition();
			character.checkCharacterCollision();
			character.makeMove();
			if(bulletRef!=null){
				bulletRef.moveBullet();
			}
		}	
	}
}